package com.hnyfkj.jyindustry.biz.sysmemberinfo.controller;

import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnyfkj.jyindustry.biz.area.entity.Area;
import com.hnyfkj.jyindustry.biz.area.service.AreaService;
import com.hnyfkj.jyindustry.biz.parameter.service.ParameterService;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.dto.SysMemberInfoDto;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.dto.SysMemberInfoInsertDto;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.dto.SysMemberInfoQueryDto;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.dto.SysMemberInfoUpdateDto;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.entity.SysMemberInfoEntity;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.sevice.SysMemberInfoService;
import com.hnyfkj.jyindustry.common.dto.RespDto;
import com.hnyfkj.jyindustry.common.utils.PageUtils;
import com.hnyfkj.jyindustry.common.utils.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zjq
 *
 * @Description
 *
 * @date 2020年12月10日
 */
@Controller
@RequestMapping("sysMemberInfo")
@Api(tags = { "用户信息" })
public class SysMemberInfoController {

	@Autowired
	private SysMemberInfoService sysMemberInfoService;
	@Autowired
	ParameterService parameterService;
	@Autowired
	private AreaService areaService;

	/**
	 * 添加页面
	 */
	@GetMapping("/add")
	public String addPage() {
		return "sys_member/member-add";
	}

	/**
	 * 编辑页面
	 */
	@GetMapping("/edit")
	public String editPage(@RequestParam("id") String id, Model model) {
		SysMemberInfoEntity sysMemberInfoEntity = sysMemberInfoService.getById(id);
		model.addAttribute("sysMemberInfoEntity", sysMemberInfoEntity);
		return "sys_member/member-edit";
	}
	
	/**
	 * 详情页面
	 */
	@GetMapping("/detail")
	public String detailPage() {
		return "sys_member/member-detail";
	}

	/**
	 * 列表页面
	 */
	@GetMapping("/listPage")
	public String listPage() {
		return "sys_member/member-table";
	}

	/*
	 * 修改密码
	 */
	@GetMapping("/setPassword")
	public String setPassword() {
		return "sys_member/set-password";
	}

	/**
	 * 列表
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "列表查看", produces = "application/json")
	public Response<PageUtils<SysMemberInfoDto>> list(@RequestBody final SysMemberInfoQueryDto queryDto) {
		PageUtils<SysMemberInfoDto> ls = sysMemberInfoService.selectListDto(queryDto);
		return Response.success(ls.getCount(), ls.getList());
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ResponseBody
	@ApiOperation(value = "新增", produces = "application/json")
	public Response save(@RequestBody final SysMemberInfoInsertDto sysMemberInfo) {
		// 密码加密
		sysMemberInfo.setSalt(DigestUtils
				.md5Hex(UUID.randomUUID().toString() + System.currentTimeMillis() + UUID.randomUUID().toString()));
		String initPassword = parameterService.getSysParameter("initPassword");
		sysMemberInfo.setPassword(sysMemberInfoService.passwordEncoder(initPassword, sysMemberInfo.getSalt()));
		sysMemberInfoService.insert(sysMemberInfo);
		return Response.success();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ResponseBody
	@ApiOperation(value = "修改", produces = "application/json")
	public RespDto<String> update(@RequestBody final SysMemberInfoUpdateDto sysMemberInfo) {
		sysMemberInfoService.update(sysMemberInfo);
		return RespDto.succ("保存成功");
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@ResponseBody
	@ApiOperation(value = "删除")
	public Response delete(@RequestParam("ids[]") final String[] memberIds) {
		sysMemberInfoService.delectBatch(memberIds);
		return Response.success();
	}

	/**
	 * @author zhujq
	 * @date 2021年3月31日 下午4:55:17
	 * @Description 普通人员完善信息
	 */
	@GetMapping("/addPerson")
	public String addPerson() {
		return "sys_member/person-add";
	}

	/**
	 * 还原密码
	 */
	@PostMapping("/initPassword")
	@ResponseBody
	@ApiOperation(value = "还原密码")
	public Response<String> initPassword(@RequestParam("id") String id) {
		return sysMemberInfoService.initPassword(id);
	}

	/**
	 * 启用
	 */
	@PostMapping("/toUse")
	@ApiOperation(value = "启用")
	@ResponseBody
	public RespDto<String> toUse(@RequestParam("id") final String id) {
		final SysMemberInfoEntity memberInfoEntity = new SysMemberInfoEntity();
		memberInfoEntity.setMemberId(id);
		memberInfoEntity.setState("0");
		sysMemberInfoService.updateById(memberInfoEntity);
		return RespDto.succ("已启用");
	}

	/**
	 * 禁用
	 */
	@PostMapping("/toUnUse")
	@ApiOperation(value = "禁用")
	@ResponseBody
	public RespDto<String> toUnUse(@RequestParam("id") final String id) {
		final SysMemberInfoEntity memberInfoEntity = new SysMemberInfoEntity();
		memberInfoEntity.setMemberId(id);
		memberInfoEntity.setState("1");
		sysMemberInfoService.updateById(memberInfoEntity);
		return RespDto.succ("已禁用");
	}

	/**
	 * @author zhujq
	 * @date 2021年4月1日 上午8:39:30
	 * @Description 根据id查信息
	 */
	@PostMapping("/getOneById")
	@ResponseBody
	@ApiOperation(value = "根据id查信息", produces = "application/json")
	public RespDto<SysMemberInfoEntity> getOneById(@RequestParam("id") String id, Model model) {
		final SysMemberInfoEntity memberInfo = sysMemberInfoService.getById(id);
		// 查询区域
		String[] areas = memberInfo.getChargeArea().split(",");
		String companyArea = "";
		for (String area : areas) {
			final Area areaArea = areaService.getOneById(area);
			companyArea += areaArea.getAreaName() + "-";
		}
		memberInfo.setChargeArea(companyArea.substring(0, companyArea.length() - 1));
		return RespDto.succResult(memberInfo);
	}
}
